//
//  ViewController.swift
//  Keyboard
//
//  Created by 周张林 on 17/8/12.
//  Copyright © 2017年 周张林. All rights reserved.
//

import UIKit

class MainViewController: UIViewController {

    var text:UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        text = UITextField.init(frame: CGRect(x:10,y:100,width:300,height:40))
        text.backgroundColor = UIColor.gray
        
        view.addSubview(text)
    
        let bl = UILabel.init(frame: CGRect(x: 5, y: 150, width: 100, height: 40))
        bl.text = "BgColor"
        bl.textColor = UIColor.white
        view.addSubview(bl)
        let btnSwitColor : UISwitch = UISwitch.init(frame: CGRect(x:100,y:150,width:150,height:40))
        view.addSubview(btnSwitColor)
        btnSwitColor.addTarget(self, action: #selector(onSwitchColor(_:)), for: .valueChanged)
        
        
        let bl2 = UILabel.init(frame: CGRect(x: 5, y: 200, width: 100, height: 40))
        bl2.text = "BgImage"
        bl2.textColor = UIColor.white
        view.addSubview(bl2)
        let btnSwitBg : UISwitch = UISwitch.init(frame: CGRect(x:100,y:200,width:150,height:40))
        view.addSubview(btnSwitBg)
        btnSwitBg.addTarget(self, action: #selector(onSwitchBg(_:)), for: .valueChanged)
        
        
        
        let btnDelBtn = UIButton.init(type: .system)
        btnDelBtn.frame = CGRect(x: 10, y: 250, width: 200, height: 40)
        btnDelBtn.backgroundColor = UIColor.white
        btnDelBtn.setTitle("Del Bg Image", for: .normal)
        view.addSubview(btnDelBtn)
        btnDelBtn.addTarget(self, action: #selector(delBgImg), for: .touchUpInside)
        
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func keyBoardWillShow(_ textField: UITextField) {
        UIView.setAnimationsEnabled(false)
    }
    func keyBoardWillHide(_ notification: Notification) {
        UIView.setAnimationsEnabled(true)
    }
    
    @objc private func onSwitchColor(_ sender:UISwitch)->Void{
        
        let shareData = ShareFileManager.getGroupShareData() ?? GroupShareData()
        
        shareData.backgroundColor = sender.isOn ? UIColor.green.hexString() : UIColor.red.hexString()
        
        ShareFileManager.saveGroupShareData(data: shareData)
        
        text?.endEditing(true)
    }
    
    @objc private func onSwitchBg(_ sender:UISwitch)->Void{
        
        let shareData = ShareFileManager.getGroupShareData() ?? GroupShareData()
        shareData.backgroundImage = "bg.png"
        ShareFileManager.saveGroupShareData(data: shareData)
        
        
        let imgData = sender.isOn ? UIImagePNGRepresentation(UIImage.init(imageLiteralResourceName: "Default/bg1.png")) : UIImagePNGRepresentation(UIImage.init(imageLiteralResourceName: "Default/bg2.png"))
        ShareFileManager.saveGroupShareFile(fileName: "bg.png", data: imgData)
        
        text?.endEditing(true)
    }

    @objc private func delBgImg(){
        if let shareData = ShareFileManager.getGroupShareData() {
            shareData.backgroundImage = nil
            
            ShareFileManager.saveGroupShareData(data: shareData)
            
            text?.endEditing(true)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

